//
//  APHomeDataManager.m
//  AqualPolo
//
//  Created by dong on 2024/5/14.
//

#import "APHomeDataManager.h"
#import "APLoginManager.h"

@interface APHomeDataManager ()
@property (nonatomic, strong) NSMutableDictionary *ballStatusRecorder;
@end

@implementation APHomeDataManager
+ (instancetype)shareInstance{
    static APHomeDataManager *shareId = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareId = [[self alloc] init];
        shareId.currentSelectedBall = 1;
    });
    return shareId;
}

- (NSMutableDictionary *)ballStatusRecorder{
    if (!_ballStatusRecorder) {
        _ballStatusRecorder = [NSMutableDictionary dictionary];
    }
    return _ballStatusRecorder;
}

- (BOOL)isBallUnlock:(APBallType)type{
    NSString *key = [self makeUserKey:[BaseDataManager ballName:type]];
    NSNumber *ballStatus = [self.ballStatusRecorder objectForKey:key];
    if (ballStatus) {
        return [ballStatus boolValue];
    }
    bool isUnlock = [[NSUserDefaults standardUserDefaults] boolForKey:key];
    self.ballStatusRecorder[key] = @(isUnlock);
    return  isUnlock;
}

- (APBallType)currentUnlockType{
    APBallType type = [[NSUserDefaults standardUserDefaults] integerForKey:[self makeUserKey:@"ballType"]];
    LYLog(@"----------APBallType-------%zd",type);
    return type;
}

- (void)unlockBall:(APBallType)type{
    if ([self isBallUnlock:type]) {
        return;
    }
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *key = [self makeUserKey:[BaseDataManager ballName:type]];
    self.ballStatusRecorder[key] = @true;
    [userDefault setBool:YES forKey:key];
    [userDefault setInteger:type forKey:[self makeUserKey:@"ballType"]];
    [userDefault synchronize];

}
- (void)updateCurrentUseBall:(APBallType)type{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setInteger:type forKey:[self makeUserKey:@"useBallType"]];
    [userDefault synchronize];
}

- (APBallType)currentUseBallType{
    APBallType type = [[NSUserDefaults standardUserDefaults] integerForKey:[self makeUserKey:@"useBallType"]];
    return type;
}

- (UIImage *)currentUseBallImage{
    return [UIImage imageNamed:[BaseDataManager ballImageName:[self currentUseBallType]]];
}

- (NSString *)currentUseBallName{
    return [NSString stringWithFormat:@"【%@】",[BaseDataManager ballName:[self currentUseBallType]]];
}

/**
 1.Novice Dancer:
 2.Intermediate Warrior:
 3.Senior Dominator:
 4.Elite Leader:
 5.Legend King:
 6.Supreme God:

 */
+ (NSString *)ballIntro:(APBallType)type{
    switch (type) {
        case APBallTypeNoviceDancer:
            return @"This designation is for beginners and emphasizes their grace and flexibility in the water, much like dance. ";
            break;
        case APBallTypeIntermediateWarrior:
            return @"This designation highlights an athlete's strength and competitiveness in the water and represents their beginning to master the basic skills of water polo.";
            break;
        case APBallTypeSeniorDominator:
            return @"This title demonstrates the leadership and dominance of athletes in water polo, and they have become the core strength of the team.";
            break;
        case APBallTypeLeader:
            return @"This title emphasizes the athlete's leadership position within the team and their ability to guide the team to victory during competition.";
            break;
        case APBallTypeLegendKing:
            return @"This title recognizes athletes who have achieved outstanding achievements in the field of water polo and have become legends in the world of water polo.";
            break;
        case APBallTypeSupremeGod:
            return @"This title represents the highest honor and status of athletes in water polo. They are not only highly skilled, but also possess outstanding leadership and influence.";
            break;
    }
}

- (void)clearData{
    NSUserDefaults *userdefault =  [NSUserDefaults standardUserDefaults];
    [userdefault removeObjectForKey:[self makeUserKey:@"useBallType"]];
    [userdefault removeObjectForKey:[self makeUserKey:@"ballType"]];
    [self.ballStatusRecorder removeAllObjects];
    for (int i = 1; i<= 6; i++) {
        NSString *key = [self makeUserKey:[BaseDataManager ballName:i]];
        [userdefault removeObjectForKey:key];
    }
}

- (NSString *)makeUserKey:(NSString *)key{
    NSString *userId = [[APLoginManager shareInstance] userInfo].userId;
    return [NSString stringWithFormat:@"%@_%@",key,userId];
}

@end
